package com.fndsoft.zheshangmeap.net.receive;

import org.json.JSONObject;

import com.fndsoft.zheshangmeap.component.colog.COLog;
import com.fndsoft.zheshangmeap.domain.ThreadMessage;
import com.fndsoft.zheshangmeap.net.HttpReturnMessage;

/**
 * @author carlos carlosk@163.com
 * @version 创建时间：2012-11-7 下午2:58:12 接收到数据的处理
 */

public class ReceiveService {

	/**
	 * 接受到数据
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-11-7 下午3:06:09
	 * @param result
	 */
	public static void receiveData(HttpReturnMessage result) {
		// 先做基础处理
		OperateBase(result);
		if (UserReceiveService.receiveData(result)) {
			return;
		}

	}

	/**
	 * 基本处理
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：Mar 16, 2014 12:03:39 PM
	 * @param result
	 */
	private static void OperateBase(HttpReturnMessage result) {
		// Gson gson = new Gson();
		ThreadMessage threadMessage = result.getThreadMessage();
		threadMessage.setReceviceIsSucces(false);
		threadMessage.setReceviceErrorCode(null);
		try {
			String jsonContent = result.getResultContent();
			JSONObject jsonObject = new JSONObject(jsonContent);
			String resultCode = jsonObject.getString("result");
			threadMessage.setReceviceErrorCode(resultCode);
			threadMessage.setReceviceIsSucces("200".equals(resultCode));
			operateResultErrorCode(threadMessage);
		} catch (Exception e) {
		}
	}

	/**
	 * 处理非成功操作返回结果
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-3-16 下午3:40:56
	 * @version
	 * @param threadMessage
	 */
	private static void operateResultErrorCode(ThreadMessage threadMessage) {
		// 重新做处理
		int resId = -1;
//		int resId = R.string.error_unknownerror;
//		String resultCode = threadMessage.getReceviceErrorCode();
//		if ("300".equals(resultCode)) {
//			resId = R.string.response_result_300;
//		} else if ("301".equals(resultCode)) {
//			resId = R.string.response_result_301;
//		} else if ("302".equals(resultCode)) {
//			resId = R.string.response_result_302;
//		} else if ("304".equals(resultCode)) {
//			resId = R.string.response_result_304;
//		} else if ("400".equals(resultCode)) {
//			resId = R.string.response_result_400;
//		} else if ("500".equals(resultCode)) {
//			resId = R.string.response_result_500;
//		} else if ("600".equals(resultCode)) {
//			resId = R.string.response_result_600;
//		} else {
//			resId = -1;
//		}
		threadMessage.setReceviceErrorResCode(resId);
	}

	/**
	 * 静态方法的debug
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2013年9月6日 下午5:58:41
	 */
	public static void debugLog(String msg) {
		COLog.d(ReceiveService.class.getSimpleName(), msg);
	}

}
